/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 15/03/2004
 * Time: 22:13:02
 */
package com.talios.jira.browser;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.talios.jira.JiraException;
import com.talios.jira.browser.signals.JiraServerDetails;
import com.talios.jira.feeds.FeedBuilder;
import com.talios.jira.feeds.QuickSearchFeedBuilder;
import com.talios.jira.feeds.SavedFilterFeedBuilder;
import com.talios.jira.idea.JiraConfiguration;
import com.talios.jira.idea.JiraConfigurationComponent;
import com.talios.jira.rpc.JiraRpcClient;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class IssueSearch {
    private JRadioButton quickSearch;
    private JRadioButton advancedSearch;
    private JComboBox filterCombo;
    private JRadioButton savedFilter;
    private JTextField searchFor;
    private JPanel mainPanel;

    private JDialog dialog;
    private JButton find;
    private JButton cancel;
    private FeedBuilder feedBuilder;
    private JComboBox serverCombo;
    private FilterComboBoxModel filterComboBoxModel;

    /**
     * @jira INTPLUG-62 Add Search by Saved Filters feature
     */
    public IssueSearch(Project project) {
        dialog = new JDialog();
        dialog.setContentPane(mainPanel);
        dialog.setTitle("Search for Issues");

        find.setIcon(IconLoader.getIcon("/actions/find.png"));
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(quickSearch);
        buttonGroup.add(advancedSearch);
        buttonGroup.add(savedFilter);

        quickSearch.setSelected(true);

        dialog.setBounds(500, 500, 500, 125);
        dialog.setModal(true);

        JiraConfigurationComponent component = project.getComponent(JiraConfigurationComponent.class);
        JiraConfiguration config = component.getConfiguration();

        final List serverList = config.getServerList();
        serverCombo.setModel(new ServerComboBoxModel(serverList));
        serverCombo.setSelectedIndex(0);
        updateFilterList(serverList);

        serverCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                savedFilter.setSelected(true);
                updateFilterList(serverList);
            }
        });

        filterCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                savedFilter.setSelected(true);
            }
        });

        find.addActionListener(new ActionListener() {
            /**
             * @jira INTPLUG-64 If quick search string is emtpy, don't search.
             */
            public void actionPerformed(ActionEvent e) {
                if (quickSearch.isSelected()) {
                    if (!"".equals(searchFor.getText())) {
                        feedBuilder = new QuickSearchFeedBuilder(searchFor.getText());
                        dialog.setVisible(false);
                    }
                }
                if (savedFilter.isSelected()) {
                    String filterName = (String) filterCombo.getSelectedItem();
                    String filterId = filterComboBoxModel.getIdOfFilter(filterName);

                    int index = serverCombo.getSelectedIndex();
                    JiraServerDetails serverDetail = (JiraServerDetails) serverList.get(index);

                    feedBuilder = new SavedFilterFeedBuilder(serverDetail, filterName, filterId);
                    dialog.setVisible(false);
                }
            }
        });

        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        });

    }

    private void updateFilterList(List serverList) {
        int index = serverCombo.getSelectedIndex();
        JiraServerDetails serverDetails = (JiraServerDetails) serverList.get(index);
        try {
            JiraRpcClient rpcClient = serverDetails.getRpcClient();
            if (rpcClient.login()) {
                filterComboBoxModel = new FilterComboBoxModel(rpcClient.getSavedFilters());
                filterCombo.setModel(filterComboBoxModel);
                find.setEnabled(true);
            }
        } catch (JiraException e1) {
            find.setEnabled(false);
        }
    }

    /**
     * @jira INTPLUG-62 Add Search by Saved Filters feature
     */
    public FeedBuilder getFeedBuilder() {
        dialog.setVisible(true);
        return feedBuilder;
    }


}